package com.face.mapper;

import com.face.entity.Visit;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface VisitMapper {

    @Select("select a.userId, max(a.loginTime) lastTime, count(*) count, b.userName from tb_visit a left join tb_user b on a.userId=b.userId " +
            "where b.shopId = #{shopId} group by userId")
    List<Map<String, Object>> getVisitCountList(String shopId);

    @Select("select a.userId, max(a.loginTime) lastTime, count(*) count, b.userName from tb_visit a left join tb_user b on a.userId=b.userId " +
            "where b.shopId = #{shopId} and a.loginTime between #{start} and #{end} group by userId")
    List<Map<String, Object>> getVisitCounts(String shopId, String start, String end);

    @Select("select * from tb_visit where shopId = #{shopId} and userId = #{userId}")
    List<Visit> findByUserId(String shopId, String userId);

    @Select("select * from tb_visit where shopId = #{shopId} and userId = #{userId} and loginTime between #{start} and #{end} order by loginTime desc")
    List<Visit> findByUserIdAndTime(String shopId, String userId, String start, String end);

    @Select("select max(a.loginTime) recentVisit, b.userId, b.group, b.type, b.userName, b.visitCount,b.imgPath from tb_visit a left join tb_user b on a.userId=b.userId where a.userId=#{userId} group by userId")
    Map<String, Object> findForWechatToRemark(String userId);

    @Select("select substr(loginTime,1,10) as date, count(userId) as count from tb_visit where shopId = #{shopId} and username != '非会员' and loginTime between #{start} and date_add(#{end},interval 1 day) group by substr(loginTime,1,10)")
    List<Map<String, Object>> statByDay(String shopId, String start, String end);

    @Select("select substr(loginTime,1,7) as date, count(userId) as count from tb_visit where shopId = #{shopId} and username != '非会员' and loginTime between #{start} and date_add(#{end},interval 1 month) group by substr(loginTime,1,7)")
    List<Map<String, Object>> statByMonth(String shopId, String start, String end);
}
